/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import core.SqlConnection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;

/**
 * Klasa pomagajaca w tworzeniu/edytowaniu tabel 
 * @author Marcin
 */
public class JTableUtil {
    /**
     * Tworzy JTable z podanego ResultSetu i nagłówkami
     * @param resultSet
     * @param columnNames tablica z nagłowkami
     * @return utworzona tabela
     */
    
    public static JTable createTable(ResultSet resultSet,String[] columnNames) throws Exception
    {
        try {
            ResultSetMetaData rsMetaData = resultSet.getMetaData();
            if(rsMetaData.getColumnCount()-1 != columnNames.length)
            {
                throw new Exception("Podano za mało nazw kolumn!");
            }
            int rowAffected = SqlConnection.getRowCount(resultSet);
            Object[][] data = new Object[rowAffected][rsMetaData.getColumnCount()];
            int j = 0;
            while(resultSet.next())
            {
                for(int i=1;i<=rsMetaData.getColumnCount();i++) {
                    data[j][i-1] = resultSet.getString(i);
                }
                j++;
            }
            return new JTable(data,columnNames){
                @Override
                public boolean isCellEditable(int rowIndex, int colIndex) {
                    return false;
                }
            };
        } catch (SQLException ex) {
            Logger.getLogger(JTableUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    /**
     * Tworzy JTable z podanego ResultSet
     * @param resultSet 
     * @return utworzona tabela albo null
     */
    public static JTable createTable(ResultSet resultSet)
    {
        try {
            ResultSetMetaData rsMetaData = resultSet.getMetaData();
            String[] columnNames = new String[rsMetaData.getColumnCount()];
            int rowAffected = SqlConnection.getRowCount(resultSet);
            Object[][] data = new Object[rowAffected][rsMetaData.getColumnCount()];
            for(int i=1;i<=rsMetaData.getColumnCount();i++) {
                columnNames[i-1] = rsMetaData.getColumnLabel(i);
            }
            int j = 0;
            while(resultSet.next())
            {
                for(int i=1;i<=rsMetaData.getColumnCount();i++) {
                    data[j][i-1] = resultSet.getString(i);
                }
                j++;
            }
            return new JTable(data,columnNames){
                @Override
                public boolean isCellEditable(int rowIndex, int colIndex) {
                    return false;
                }
            };
        } catch (SQLException ex) {
            Logger.getLogger(JTableUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
}
